<template>
  <div class="student-info">
    <el-col :offset="8" :span="8">
      <el-form :model="props.studentInfo" label-width="100px">
        <el-form-item label="姓名">
          <el-input v-model="props.studentInfo.name" readonly></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="props.studentInfo.age" readonly></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input :value="props.studentInfo.gender === 1 ? '男' : '女'" readonly></el-input>
        </el-form-item>
        <el-form-item label="学号">
          <el-input v-model="props.studentInfo.studentId" readonly></el-input>
        </el-form-item>
        <el-button type="success" @click="editStudent" style="margin-left: 250px;">修改个人信息</el-button>
      </el-form>
    </el-col>
  </div>
  <el-dialog v-model="dialogStudentInfoVisible" title="修改学生信息" width="40%" top="20vh" center>
    <el-form :model="editStudentInfo" label-width="100px" ref="form">
      <el-form-item label="学生ID" prop="id">
        <el-input v-model="editStudentInfo.id" placeholder="请输入学生ID" type="number" disabled></el-input>
      </el-form-item>
      <el-form-item label="学生姓名" prop="name">
        <el-input v-model="editStudentInfo.name" placeholder="请输入学生姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别(男1,女2)" prop="gender">
        <el-input v-model="editStudentInfo.gender" placeholder="请输入学生性别(男填1，女填2)" type="number"></el-input>
      </el-form-item>
      <el-form-item label="学生年龄" prop="age">
        <el-input v-model="editStudentInfo.age" placeholder="请输入学生年龄" type="number"></el-input>
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input v-model="editStudentInfo.password" placeholder="如须修改密码请输入新密码，如不修改请留空" type=show-password></el-input>
      </el-form-item>
      <el-form-item label="学生学号" prop="studentId">
        <el-input v-model="editStudentInfo.studentId" placeholder="请输入学生学号" type="number"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogStudentInfoVisible = false">取消修改</el-button>
        <el-button type="primary" @click="handleEdit()">
          确认修改
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive } from 'vue'
import { defineProps, ref, defineEmits } from 'vue';
import { studentUpdateService } from '@/api/student.js';
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores'
let props = defineProps(["studentInfo"])
const emit = defineEmits(['getStudentInfo'])
const userStore = useUserStore()
const dialogStudentInfoVisible = ref(false);
console.log(props.studentInfo)
const editStudentInfo = reactive({
  name: '',
  gender: '',
  age: '',
  studentId: '',
  password: '',
  id: userStore.id
});
const editStudent = () => {
  dialogStudentInfoVisible.value = true;
  editStudentInfo.name = props.studentInfo.name;
  editStudentInfo.gender = props.studentInfo.gender;
  editStudentInfo.age = props.studentInfo.age;
  editStudentInfo.studentId = props.studentInfo.studentId;
  editStudentInfo.password = props.studentInfo.password;
}
const handleEdit = async () => {
  if (editStudentInfo.name && editStudentInfo.gender && editStudentInfo.age && editStudentInfo.studentId) {
    const res = await studentUpdateService(editStudentInfo)
    emit('getStudentInfo')
    ElMessage.success('修改成功!')
    dialogStudentInfoVisible.value = false
    editStudentInfo.name = ''
    editStudentInfo.gender = ''
    editStudentInfo.age = ''
    editStudentInfo.studentId = ''
    editStudentInfo.password = ''
  } else {
    ElMessage.error('请填写完整信息!')
  }
}
</script>

<style scoped>
.student-info {
  padding: 20px;
}
</style>
